package insat.challenge;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainTestPost extends  Activity implements OnClickListener{

	
	String codesource="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		Log.i("avant la lecture","avant la lecture");
		Button bt = (Button)findViewById(R.id.button_test);
		bt.setOnClickListener(this);
		Log.i("apres lecture","apres lecture");
		
	}

	public String GetHTML( List <NameValuePair> nvps) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		String url="http://helpbox.belhassineamine.com/index.php/api/getplacesforcategory.json";
		try {
			HttpResponse res;
			URI uri = new URI(url);
			if (nvps!=null){
				HttpPost methodpost = new HttpPost(uri);
				methodpost.addHeader("pragma","no-cache");
				methodpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
				res = httpClient.execute(methodpost);
			} else {
				HttpGet methodget = new HttpGet(uri);
				methodget.addHeader("pragma","no-cache");
				res = httpClient.execute(methodget);
			}
			InputStream data = res.getEntity().getContent();

			return generateString(data);

		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}
	

	static public String generateString(InputStream stream) {
		InputStreamReader reader = new InputStreamReader(stream);
		BufferedReader buffer = new BufferedReader(reader, 4*1024);
		StringBuilder sb = new StringBuilder();
		try { 
			String cur;   
			while ((cur = buffer.readLine()) != null) {   
				sb.append(cur).append("\n");  
			}  
		} catch (IOException e) {  
			e.printStackTrace();  
		}
		try {
			stream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString(); 
	}

	void lecture(){
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); //On crée la liste qui contiendra tous nos paramètres
		/*
		  
		  Les paamètre de la req POST
		      -longitude
              -latitude
              -category_id
              -rayon*/
		
		nameValuePairs.add(new BasicNameValuePair("longitude", "36.843104"));
		nameValuePairs.add(new BasicNameValuePair("latitude", "10.196428"));
		nameValuePairs.add(new BasicNameValuePair("category_id", "1"));
		nameValuePairs.add(new BasicNameValuePair("rayon", "1000"));
		codesource=GetHTML(nameValuePairs);
		Log.i("codesource",codesource);
	}

	@Override
	public void onClick(View v) {
		Log.i("avant la lecture","ds btavant lec");
	lecture();
		
	}
	
}